/*  Neu - Feed Aggregator.
 *  Copyright (C) 2009  Joshua Montgomery
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details. 
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.neu.core;

import java.io.FileNotFoundException;
import java.net.URISyntaxException;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.neu.config.Config;
import com.neu.exceptions.ImproperConfigFile;
import com.neu.gui.GUIManager;

/** Starts the Application.
 * 
 * @author Joshua Montgomery
 * @created May 6, 2009
 */
public class Main 
{
	/**Logger*/
	private static Logger logger =  Logger.getLogger(Main.class);
	
	/**GUI Manager for the application.*/
	private GUIManager guiManager;
	
	/**Sets up and initializes the application.*/
	public void init()
	{
		//SETUP
		PropertyConfigurator.configure(Config.class.getResource("/com/neu/config/log4jConfig.txt"));
		
		logger.debug("Loading config file...");
		try {
			Config.configure("config.txt");//try for local copy
		} catch (FileNotFoundException e) 
		{
			logger.debug("Couldn't not find a local copy of the Config file. \nNow loading the default Config file...");
		   	try
			{
				Config.configure(Config.class.getResource("/com/neu/config/config.txt").toURI());
			}catch (URISyntaxException e1)
			{
				//shouldn't happen
				e.printStackTrace();
				System.exit(1);
			}catch (FileNotFoundException e1) 
		   	{    		
		   		logger.error("A config file could not be found! \nShutting Down...", e1);
		   		System.exit(1);
		   	}catch (ImproperConfigFile e1)
			{
				logger.debug("The Config file was invalid. \nShutting Down...", e1);
				System.exit(1);
			}  	
		}catch (ImproperConfigFile e) {			
			logger.debug("The Config file was invalid. \nShutting Down...", e);
			System.exit(1);
		}	  
		
		if(Config.isDebugModeEnabled())
			logger.debug("Debug Mode Enabled!");
		else			
			Logger.getRootLogger().setLevel(Level.INFO);
		
		//INITIALIZE
		guiManager = new GUIManager();
	}
	
	/** Starts the application.
	 * 
	 * @param args Command line arguments
	 */
	public static void main(String[] args) 
	{
		Main main = new Main();
		main.init();
	}

}
